import { hilog } from '@kit.PerformanceAnalysisKit'
import { FnContext } from '../../../../Index';
import { generateMsg2 } from './LogUtil';

function generateMsg(fileName: string, functionName: string, action: string, status: ActionState | string,
  detail: string = "", ...extraModuleName: string[]) {
  let msg = "";
  if (extraModuleName && extraModuleName.length) {
    msg += `[${extraModuleName.join("")}]`
  }
  msg += `[${fileName}/${functionName}] [${action}] [${status}]`;
  if (detail) {
    msg += ` [${detail}]`;
  }
  return msg;
}

/**
 * @deprecated
 * @param fileName
 * @param funName
 * @param action
 * @param state
 * @param detail
 * @param tags
 */
export function _appInfo(fileName: string, funName: string, action: string, state: ActionState | string, detail?: string,
  ...tags: string[]) {
  let message = generateMsg(fileName, funName, action, state, detail, ...tags);
  hilog.info(0x00, "__app", message);
}

export function _appInfo3(fileName: string, funName: string, detail?: string,
  ...tags: string[]) {
  let message = generateMsg(fileName, funName, "", "", detail, ...tags);
  hilog.info(0x00, "__app", message);
}


export interface LogExtra {
  fileName?: string,
  funName?: string,
  action?: string,
  state?: ActionState | string,
  tags?: string[]
  //匹配的日志不打印
  ignorePattern?: string
  //如果使用了ignorePattern,则会创建一个map,在第logCount时打印一次
  logCount?: number
  c?: FnContext
}


export function _appError2(content: string, extra?: LogExtra) {
  if (!extra) {
    extra = { tags: [] };
  }
  extra.tags.push(LogTag.Error, LogTag.VV);
  const message = generateMsg2(content, extra);
  hilog.info(0x00, "__app", message);
}

/**
 * @param fileName
 * @param functionName
 * @param action
 * @param status
 * @param detail
 * @param extraModuleName
 */
export function _appError(fileName: string, functionName: string, action: string, status: ActionState | string,
  detail: string = "", ...extraModuleName: string[]) {
  let message = generateMsg(fileName, functionName, action, status, detail, ...extraModuleName, LogTag.Error, LogTag.VV);
  hilog.error(0x00, "__app", message);
}


export function _sInfo(fileName: string, funName: string, detail: string) {
  let msg = `[${fileName}/${funName}] [${detail}]`
  hilog.info(0x00, "__app", msg);
}

export enum LogTag {
  Login = "__login",
  App = "__App",
  Error = "__Error",
  V = "__V",
  Db = "Db__",
  BrickDb = "BrickDb__",
  VV = "__VV",
  Life = "__Life",
  SelectFolder = "__SelectFolder",
  ChangeFolder = "ChangeFolder__",
  BPart = "BPart__",
  CPart = "CPart__",
  Bmp = "__Bmp",
  GifFrame = "__GifFrame", //解码Gif一帧的主流程
  QDic = "QDic__",
  FileService = "FileService__",
  GifGen = "__GifGen", //解析Gif的主流程
  GifLwz = "__GifLwz",
  GifPlay = "__GifPlay",
  GameGen = "__GameGen",
  GameUserInput = "__GameUserInput",
  Debug = "Debug__",
  DbViewer = "DbViewer__",
  FolderCache = "FolderCache"
}

export enum ActionState {
  After = "After", //动作结束之后记录,不确定是否成功,或者没有明显的表示成功的数据.
  Start = "Start", //动作开始之前记录
  Suc = "Suc", //动作成功
  Fail = "Fail", // 动作失败.
  None = "",
  Fin = "Fin", //动作结束之后记录,不确定是否成功,或者没有明显的表示成功的数据.
  Step = "Step"
}